package com.laber.book.controller;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.socket.TextMessage;

import com.laber.book.socket.SocketHandler;

/**
 * @desp Socket控制器
 * @author liulichao@ruc.edu.cn
 * @date 2016-5-6
 *
 */
@Controller
public class BookWebSocketController{
  
  private static final Logger logger = LoggerFactory.getLogger(BookWebSocketController.class);
  
  @Autowired
  private SocketHandler socketHandler;
  
  @RequestMapping(value="/login")
  public String login(HttpSession session){
    logger.info("用户登录了建立连接啦");
    
    session.setAttribute("user", "liulichao");
    
    return "home";
  }

  @RequestMapping(value = "/message", method = RequestMethod.GET)
  public String sendMessage(){
    
    socketHandler.sendMessageToUser("liulichao", new TextMessage("这是一条测试的消息"));
    
    return "message";
  }

}